package com.itheima.pinda.authority.config;

import com.itheima.pinda.authority.biz.service.common.MyLoginLogService;
import com.itheima.pinda.authority.biz.service.common.OptLogService;
import com.itheima.pinda.log.entity.OptLogDTO;
import com.itheima.pinda.log.event.LoginListener;
import com.itheima.pinda.log.event.SysLogListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.function.Consumer;

/**
 * 系统操作日志配置类
 *
 * @author llhuang10
 * @date 2021/8/27 17:17
 */
@Slf4j
@Configuration
@EnableAsync
public class SysLogConfiguration {


    /**
     * 创建操作日志记录监听器对象
     *
     * @param optLogService
     * @return SysLogListener
     */
    @Bean
    public SysLogListener sysLogListener(OptLogService optLogService) {
        Consumer<OptLogDTO> consumer = (optLogDTO -> optLogService.save(optLogDTO));
        return new SysLogListener(consumer);
        // return new SysLogListener(optLogDTO -> {
        //     optLogService.save(optLogDTO);
        // });
    }

    /**
     * 创建登录日志记录监听器对象
     *
     * @param myLoginLogService
     * @return LoginListener
     */
    @Bean
    public LoginListener loginListener(MyLoginLogService myLoginLogService) {
        // todo 这里为什么没打印日志, 还是在项目启动之初就打印了日志了?
        //  确实是启动之初打印日志, 并不是每次记录一次登录日志打印一次日志
        // 2021-08-28 11:39:18.804:[ INFO] [main:11714] [c.i.pinda.authority.config.SysLogConfiguration.loginListener:51]
        // --> =================== 执行创建LoginListener监听器对象================
        log.info("=================== 执行创建LoginListener监听器对象================");
        return new LoginListener(loginLogDTO -> {
            myLoginLogService.save(loginLogDTO);
        });
    }

}
